//
//  DoggyPresenter.swift
//  MVP_Demo
//
//  Created by mac on 2017/7/28.
//  Copyright © 2017年 LLyan. All rights reserved.
//

import Foundation

class DoggyPresenter {
    
    //MARK: - Private
    fileprivate let dogService: DoggyService
    weak fileprivate var dogView: DoggyView?
    
    init(dogService: DoggyService) {
        self.dogService = dogService
    }
    
    func attachView(_ attach: Bool, view: DoggyView?) {
        if !attach {
            dogView = nil
        }else{
            if let view = view {
                dogView = view
            }
        }
    }
    
    func getDogs() {
        self.dogView?.startLoading()
        
        dogService.deliverDoggies { [weak self] doggies in
            self?.dogView?.finishLoading()
            
            if doggies.count == 0 {
                self?.dogView?.setEmpty()
            }else{
                self?.dogView?.setDoogies(doggies.map({
                    return DoggyViewData(name: "\($0.name) \($0.breed)", age: "\($0.age)")
                }))
            }
            
        }
    }
    
}

struct DoggyViewData {
    let name: String
    let age: String
}
